﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class uc_main_center : DAO.ESWebUserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitializeUI();
        LoadPage();
        LoadCountry();
        LoadAppTypes(this.LangId);
        LoadPlacementTypes(this.LangId);
        LoadCompanyType(this.LangId);
    }

    protected override void InitializeUI()
    {
        long langId = DAO.Connection.DEFAULT_LANGUAGE;
        if (Session["language"] != null)
            long.TryParse(Session["language"].ToString(), out langId);

        this.LangId = Convert.ToInt32(langId);
    }

    private void LoadPage()
    {
        string text;
        string keywords;
        Utilities.LoadPage(this.LangId, "home", out text, out keywords);
        lblText.Text = text;
    }

    private void LoadAppTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc') WHERE isActive = 'true'", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 12);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddl_type_offer.Items.Clear();
                ddl_type_offer.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddl_type_offer.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddl_type_offer.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadCountry()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT id, short, name FROM dbo.Fn_GetAllPlaces(0, 1000000, 'id', 'asc', 2) WHERE isActive='true' Order By name asc", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddl_country.Items.Add(new ListItem("", "0"));
                dd_country.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    ddl_country.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                    dd_country.Items.Add(new ListItem(dr["name"].ToString() + " - " + dr["short"].ToString(), dr["id"].ToString()));
                }
                dr.Close();
            }
        }
    }

    private void LoadPlacementTypes(Int64 langId)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from dbo.Fn_GetTypeByType(@language_id,@type_id,0,30,'id','desc')", conn))
            {
                SqlParameter[] pars = new SqlParameter[2];
                pars[0] = new SqlParameter("language_id", langId);
                pars[1] = new SqlParameter("type_id", 8);

                foreach (SqlParameter s in pars)
                    cmd.Parameters.Add(s);
                SqlDataReader dr = cmd.ExecuteReader();
                ddl_placementType.Items.Clear();
                ddl_placementType.Items.Add(new ListItem("", "0"));
                while (dr.Read())
                {
                    if (LangId == DAO.Connection.DEFAULT_LANGUAGE)
                    {
                        ddl_placementType.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                    else
                    {
                        ddl_placementType.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }

    private void LoadCompanyType(int lang)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * FROM dbo.Fn_GetAllUniversalTypes(0, 1000000, 'id', 'asc', 2, " + lang + ") WHERE isActive='true'", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                ddl_CompanyType.Items.Add(new ListItem("", "0"));
                if (lang == DAO.Connection.DEFAULT_LANGUAGE)
                {
                    while (dr.Read())
                    {
                        ddl_CompanyType.Items.Add(new ListItem(dr["name"].ToString(), dr["id"].ToString()));
                    }
                }
                else
                {
                    while (dr.Read())
                    {
                        ddl_CompanyType.Items.Add(new ListItem(dr["name"].ToString(), dr["parent_id"].ToString()));
                    }
                }
                dr.Close();
            }
        }
    }
}
